<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div class="contaner">我是div</div>
    <button>点击</button>
    <script>
      // 一、innerHTML:可以替换元素的超文本内容
      // 1.获取元素 2.操作元素；
      var contanerEle = document.getElementsByClassName("contaner")[0];
      var btn = document.getElementsByTagName("button")[0];
      btn.onclick = function () {
        // contanerEle.innerHTML = "<h1>我是替换的内容</h1>";
        // var name = "张三";
        // contanerEle.innerHTML = "<p>我的名字是"+name+"</p>";

        // 1.通过数组 循环出html结构填充到页面里面
        // var arr = ["张三","李四","王五"];
        // var htmlstr = "<ul>";
        // for(var i=0;i<arr.length;i++){
        //     htmlstr += "<li>我的名字是"+arr[i]+"</li>";
        // }
        // htmlstr += "</ul>";
        // console.log(htmlstr);
        // contanerEle.innerHTML = htmlstr;

        // 2.通过对象数组 来替换 html元素里的内容；
        var arr = [
          { name: "张三", age: 20, sex: "男" },
          { name: "李四", age: 22, sex: "女" },
          { name: "王五", age: 21, sex: "男" },
          { name: "王小六", age: 23, sex: "男" },
        ];
        var htmlstr = "<ul>";
        for (var i = 0; i < arr.length; i++) {
          // console.log(arr[i])  //   { name: "张三", age: 20, sex: "男" }
          htmlstr += `<li style='background:red'>姓名是${arr[i].name};年龄是 ${arr[i].age} 性别是${arr[i].sex}</li>`;
        }

        htmlstr += "</ul>";

        contanerEle.innerHTML = htmlstr;
      };
    </script>
  </body>
</html>
